package com.designpatterns.decorator2;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

/**
 * 使用装饰者模式，在过滤器中解决乱码问题
 */
public class NewDecorator extends HttpServletRequestWrapper {

    private HttpServletRequest request;
    public NewDecorator(HttpServletRequest request) {
        super(request);
        this.request = request;
    }

    //直接增强单值api和多值api
    @Override
    public String getParameter(String name) {
        String value = request.getParameter("name");
       /* try {
            value = new String(value.getBytes("ISO-8859-1"), StandardCharsets.UTF_8);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }*/
       value = "234";
        return value;
    }

    @Override
    public String[] getParameterValues(String name) {
        String[] values = request.getParameterValues("name");
        /*for (int i = 0; i < values.length; i++) {
            try {
                values[i] = new String(values[i].getBytes("ISO-8859-1"),"utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }*/
        return values;
    }
}
